<template>
  <div>
    <!-- ods line: {{ records_ods_line }} -->

    <a-collapse>
      <a-collapse-panel key="1" header="原单明细">
        <a-table
          :columns="columns_ods_line"
          :data-source="records_ods_line"
          :pagination="pagination"
          rowKey="id"
        >
        </a-table>
      </a-collapse-panel>
    </a-collapse>

    <div>invoice_type:{{ values.invoice_type }}</div>

    <div>merge_type:{{ values.merge_type }}</div>
    <div>merge_check: {{ values.merge_check }}</div>

    <!-- <a-divider type="vertical" /> -->
    <!-- <a-button @click="handleMergeType('push_plus')"> 直接推送 </a-button> -->
    <a-divider type="vertical" />
    <a-button @click="handleMergeType('split_discount')"> 折扣分摊 </a-button>
    <a-divider type="vertical" />
    <a-button @click="handleMergeType('merge_plus')"> 品种合并 </a-button>
    <a-divider type="vertical" />
    <a-button @click="handleMergeType('merge_neg')"> 正负合并 </a-button>

    so line

    <a-divider type="vertical" />
    <a-divider type="vertical" />
    <a-divider type="vertical" />
    <a-button @click="create_sale_order"> 创建销售订单 </a-button>

    <!-- {{ records_line }} -->
    <a-table
      :columns="columns_line"
      :data-source="records_line"
      :pagination="pagination"
      rowKey="id"
    >
    </a-table>
  </div>
</template>

<script>
import api from '@/odoorpc'

export default {
  name: 'Ods2So',
  components: {},
  mixins: [],
  props: {},

  data() {
    return {
      view: undefined,
      values: {},
      records_ods_line: [],
      records_line: [],
      pagination: {
        // position: 'top'
        total: 0,
        pageSize: 3,
        showSizeChanger: true
      }
    }
  },
  computed: {
    columns_ods_line() {
      return [
        // {
        //   dataIndex: 'id',
        //   key: 'id',
        //   title: 'ID',
        //   ellipsis: true
        // },

        {
          dataIndex: 'order_num',
          key: 'order_num',
          title: '订单编号',
          ellipsis: true
        },
        {
          dataIndex: 'order_partner_id',
          key: 'order_partner_id',
          title: '客户',
          ellipsis: true
        },
        {
          dataIndex: 'vat_product_name',
          key: 'vat_product_name',
          title: '税务商品',
          ellipsis: true
        },
        {
          dataIndex: 'product_name',
          key: 'product_name',
          title: '商品名称',
          ellipsis: true
        },
        {
          dataIndex: 'product_spec',
          key: 'product_spec',
          title: '规格',
          ellipsis: true
        },

        {
          dataIndex: 'product_uom',
          key: 'product_uom',
          title: '单位',
          ellipsis: true
        },
        // {
        //   dataIndex: 'tax_id',
        //   key: 'tax_id',
        //   title: '税率',
        //   ellipsis: true
        // },
        {
          dataIndex: 'quantity',
          key: 'quantity',
          title: '数量',
          ellipsis: true
        },
        {
          dataIndex: 'price_unit',
          key: 'price_unit',
          title: '单价',
          ellipsis: true
        },

        {
          dataIndex: 'amount_untaxed',
          key: 'amount_untaxed',
          title: '不含税金额',
          ellipsis: true
        },
        {
          dataIndex: 'amount_tax',
          key: 'amount_tax',
          title: '税',
          ellipsis: true
        },
        {
          dataIndex: 'amount_total',
          key: 'amount_total',
          title: '含税金额',
          ellipsis: true
        }
      ]
    },
    columns_line() {
      return [
        {
          dataIndex: 'order_num',
          key: 'order_num',
          title: '业务销售单号',
          ellipsis: true
        },
        {
          dataIndex: 'order_partner_id',
          key: 'order_partner_id',
          title: '客户',
          ellipsis: true
        },
        {
          dataIndex: 'vat_product_name',
          key: 'vat_product_name',
          title: '税务商品',
          ellipsis: true
        },
        {
          dataIndex: 'product_name',
          key: 'product_name',
          title: '商品名称',
          ellipsis: true
        },
        {
          dataIndex: 'product_spec',
          key: 'product_spec',
          title: '规格',
          ellipsis: true
        },
        {
          dataIndex: 'quantity',
          key: 'quantity',
          title: '数量',
          ellipsis: true
        },
        {
          dataIndex: 'product_uom',
          key: 'product_uom',
          title: '单位',
          ellipsis: true
        },
        {
          dataIndex: 'tax_ratio',
          key: 'tax_ratio',
          title: '税率',
          ellipsis: true
        },
        {
          dataIndex: 'price_unit',
          key: 'price_unit',
          title: '含税单价',
          ellipsis: true
        },
        {
          dataIndex: 'amount_untaxed',
          key: 'amount_untaxed',
          title: '无税金额',
          ellipsis: true
        },
        {
          dataIndex: 'amount_tax',
          key: 'amount_tax',
          title: '税额',
          ellipsis: true
        },
        {
          dataIndex: 'amount_total',
          key: 'amount_total',
          title: '含税金额',
          ellipsis: true
        },
        {
          dataIndex: 'amount_discount',
          key: 'amount_discount',
          title: '折扣',
          ellipsis: true
        }
      ]
    }
  },

  watch: {},

  async created() {},

  async mounted() {},

  methods: {
    async _initMe_relation(view, fields_list) {
      const fields = view.fields
      const relations = {}
      for (const fld of fields_list) {
        const field_info = fields[fld]
        const relation = api.env.relation(field_info, {
          parent: view.view_info
        })

        await relation.load_views()

        relations[fld] = relation
      }
      view.load_relations_done(relations)
    },

    async _initMe_view(activeIds) {
      const view = api.env.wizardview('fp_sale.action_ods_to_sale_order', {
        active_ids: activeIds
      })
      // const fields =
      await view.load_fields()
      await this._initMe_relation(view, ['line_ids', 'ods_line_ids'])

      return view
    },

    async _initMe_data(view, { context }) {
      const res = await view.onchange_new({ context })
      console.log(res)
      const { values } = res

      return values
    },

    async _initMe_data_relation(view, values, fields_list) {
      const result = {}
      for (const fld of fields_list) {
        // console.log(fld)
        const relation_value = values[fld]
        // console.log(relation_value, view.relations)
        const relation_records = await view.relations[
          fld
        ].tree.read_for_new_x2m(relation_value)

        result[fld] = relation_records
      }

      return result
    },

    async initMe(activeIds, { context }) {
      console.log(context)
      const view = await this._initMe_view(activeIds)
      const values = await this._initMe_data(view, { context })

      this.view = view

      this.values = values

      const realtion_data = await this._initMe_data_relation(view, values, [
        'line_ids',
        'ods_line_ids'
      ])

      console.log(values, realtion_data)

      this.records_ods_line = realtion_data.ods_line_ids
      this.records_line = realtion_data.line_ids
    },

    async handleMergeType(merge_type) {
      console.log('handleMergeType', merge_type)
      // console.log(value, ...args)
      // console.log(this.invoice_type_value)
      const { values } = await this.view.onchange('merge_type', {
        value: merge_type
      })

      this.values = values

      const realtion_data = await this._initMe_data_relation(
        this.view,
        values,
        ['line_ids', 'ods_line_ids']
      )

      this.records_ods_line = realtion_data.ods_line_ids
      this.records_line = realtion_data.line_ids

      console.log(realtion_data)
      console.log(this.view)

      // console.log(res)

      // const ods_ids = await this._nc2ods(this.activeIds)
      // this.$emit(
      //   'change-menu',
      //   'fp_sale.menu_action_fp_ods_sale_order',
      //   ods_ids
      // )
    },
    async create_sale_order() {
      await this.view.wizard_button_clicked({
        type: 'object',
        name: 'create_sale_order'
      })

      this.$emit('ods2so-done')
    }
  }
}
</script>

<style type="text/css" scoped></style>
